﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Attributes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Drawing;

namespace Perovich.GameObjects.Components
{
    [ComponentDependency(typeof(Position2D))]
    [AutomaticProperty("Texture","Texture.Name", XmlSummary = "Gets or sets the name of the texture resource.", XmlReturns = "A <see cref=\"System.String\">string</see> naming the texture resource to load.")]
    [AutomaticProperty("TextureOrigin", "Texture.Origin", XmlSummary = "Gets or sets the offset the texture will be drawn at.", XmlReturns = "A <see cref=\"Vector2\">Vector2</see> representing wheret eh sprite is drawn in relation to the position of the object.")]
    [AutomaticImport("Microsoft.Xna.Framework.Graphics", true)]
    public sealed class Texture : DrawableEntityComponent, ISizeable
    {
        Position2D position;
        Size2D size;
        Texture2D tex;
        Transform2D trans;

        public Texture2D Tex
        {
            get { return tex; }
        }
        string tex_name;
        bool loaded = false;

        public Texture()
        {
            Origin = Vector2.Zero;
        }

        public string Name
        {
            get
            {
                return tex_name;
            }
            set
            {
                tex_name = value;
                if (loaded)
                {
                    tex = Owner.Content.Load<Texture2D>(tex_name);
                }
            }
        }

        public int Width
        {
            get
            {
                return tex.Width;
            }
        }

        public int Height
        {
            get
            {
                return tex.Height;
            }
        }

        public Size Size
        {
            get
            {
                return new Size(Width, Height);
            }
        }

        public Vector2 Origin { get; set; }

        public override void Initialize()
        {
            position = Owner.Find<Position2D>();
            Owner.ComponentAdded += new EventHandler<EntityComponentEventArgs>(Owner_ComponentAdded);
            Owner.ComponentRemoved += new EventHandler<EntityComponentRemovedEventArgs>(Owner_ComponentRemoved);
            size = Owner.Find<Size2D>();
            trans = Owner.Find<Transform2D>();
            base.Initialize();
        }

        void Owner_ComponentRemoved(object sender, EntityComponentRemovedEventArgs e)
        {
            if (e.Component is Size2D)
                size = null;
            if (e.Component is Transform2D)
                trans = null;
        }

        void Owner_ComponentAdded(object sender, EntityComponentEventArgs e)
        {
            Size2D c;
            Transform2D t;
            if ((c = e.Component as Size2D) != null)
                size = c;
            if ((t = e.Component as Transform2D) != null)
                trans = t;
        }

        public override void LoadContent()
        {
            tex = Owner.Content.Load<Texture2D>(tex_name);
            loaded = true;
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            float depth = 0;
            float scale = 0;
            float rotation = 0;
            if (trans != null)
            {
                depth = trans.Depth;
                scale = trans.Scale;
                rotation = trans.Rotation;
            }

            if (size != null)
            {
                Owner.SBatches.CurrentBatch.Draw(tex, position.Position, null, Microsoft.Xna.Framework.Color.White, rotation, Vector2.Zero, new Vector2(scale), SpriteEffects.None, depth);
            }
            else
            {
                Owner.SBatches.CurrentBatch.Draw(tex, position.Position, null, Microsoft.Xna.Framework.Color.White, rotation, Vector2.Zero, new Vector2(scale), SpriteEffects.None, depth);
            }
            base.Draw(gameTime);
        }

    }
}
